import netCtr from "../ctrs/netCtr";
import treasureCtr from "../ctrs/treasure/treasureCtr";
import treasureEntity from "../ctrs/treasure/treasureEntity";
import BaseGameComponent from "../libs/core/BaseGameComponent";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { getTimehms } from "../libs/core/utils";
import { INotification } from "../libs/pureMvc/Interfaces";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-14 16:11:21
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-14 17:33:45
 */
const { ccclass, property } = cc._decorator

@ccclass

export default class bxDescCom extends BaseGameComponent {

  @property({ type: cc.Node, tooltip: "三角节点" })
  sjNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "描述节点" })
  descNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "奖励1节点" })
  rewardNode1: cc.Node = null;

  @property({ type: cc.Label, tooltip: "奖励1描述" })
  reward1Txt: cc.Label = null;

  @property({ type: cc.Node, tooltip: "奖励2节点" })
  rewardNode2: cc.Node = null;

  @property({ type: cc.Label, tooltip: "奖励2描述" })
  reward2Txt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "开启按钮描述" })
  lqBtnTxt: cc.Label = null;

  @property({ type: cc.Node, tooltip: "加速按钮" })
  jsBtn: cc.Node = null;

  @property({ type: cc.Label, tooltip: "加速所需钻石" })
  needD: cc.Label = null;

  private _treasureEny: treasureEntity = null;

  private _key;

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_TREASURE_UDPATE, PureMVCEvents.ON_TREASURE_CLOSE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    switch (evtName) {
      case PureMVCEvents.ON_TREASURE_UDPATE:
        this.drawUi(this._key);
        break;
      case PureMVCEvents.ON_TREASURE_CLOSE:
        // this.node.active = false;
        break;
    }
  }

  drawUi(key) {
    this._key = key;
    let list: treasureEntity[] = treasureCtr.ins.treasureEnyDic.getValueList();

    this._treasureEny = list[key];

    if (!this._treasureEny) {
      this.node.active = false;
      return;
    }

    switch (key) {
      case "0":
        this.sjNode.y = 135;
        this.descNode.y = 60;
        break;
      case "1":
        this.sjNode.y = 35;
        this.descNode.y = 0;
        break;
      case "2":
        this.sjNode.y = -80;
        this.descNode.y = 0;
        break;
      case "3":
        this.sjNode.y = -190;
        this.descNode.y = -80;
        break;
    }

    this.rewardNode1.active = true;
    this.rewardNode2.active = true;
    this.rewardNode1.x = -55;
    this.rewardNode2.x = 55;
    switch (list[key].treasureId) {
      case 1:
        this.rewardNode1.active = true;
        this.rewardNode2.active = false;
        this.rewardNode1.x = 0;
        this.reward1Txt.string = "5-25钻石"
        break;
      case 2:
        this.reward1Txt.string = "20-100钻石";
        this.reward2Txt.string = "随机碎片x1";
        break;
      case 3:
        this.reward1Txt.string = "100钻石";
        this.reward2Txt.string = "随机碎片x1";
        break;
      case 4:
        this.reward1Txt.string = "200钻石";
        this.reward2Txt.string = "随机碎片x4";
        break;
    }
    this.jsBtn.active = false;
    if (list[key].step == 1) {
      this.lqBtnTxt.string = "开启宝箱";
    } else if (list[key].step == 2) {
      let now = new Date().getTime();
      this.lqBtnTxt.string = this.lqBtnTxt.string = "距离开启时间\n" + getTimehms(this._treasureEny.openTime - Math.ceil(now / 1000));;
      this.jsBtn.active = true;
      let needD = Math.ceil((this._treasureEny.openTime -(now / 1000)) / 60) ;
      this.needD.string = `x${needD}`;
      if (Math.ceil(now / 1000) > this._treasureEny.openTime) {
        this.lqBtnTxt.string = "领取奖励";
        this.jsBtn.active = false;
      }
    } else if (list[key].step == 3) {
      this.lqBtnTxt.string = "领取奖励";
    }
  }

  onBtnClick() {
    if (this._treasureEny.step == 1) {
      netCtr.ins.openTreasure(this._treasureEny.treasureId, this._treasureEny.uid);
    } else if (this._treasureEny.step == 2) {

      let now = new Date().getTime();
      if (Math.ceil(now / 1000) > this._treasureEny.openTime) {

        netCtr.ins.getReward(this._treasureEny.treasureId, this._treasureEny.uid);
      }
    } else if (this._treasureEny.step == 3) {

      netCtr.ins.getReward(this._treasureEny.treasureId, this._treasureEny.uid);
    }
  }

  /**
   * 加速
   */
  onJsClick() {
    if (this._treasureEny.step == 2) {
      let now = new Date().getTime();
      if (Math.ceil(now / 1000) < this._treasureEny.openTime) {
        let needD = Math.ceil((this._treasureEny.openTime -(now / 1000)) / 60) ;
        netCtr.ins.accelerateTreasure(this._treasureEny.treasureId, this._treasureEny.uid, needD);
      }
    }
  }

  private _interval: number = 0;

  protected update(dt: number): void {
    this._interval++;
    if (this._interval > 60) {
      this._interval = 0;
      if (this._treasureEny && this._treasureEny.step == 2) {
        let now = new Date().getTime();
        if (Math.ceil(now / 1000) < this._treasureEny.openTime) {
          this.lqBtnTxt.string = "距离开启时间\n" + getTimehms(this._treasureEny.openTime - Math.ceil(now / 1000));
        } else {
          this.lqBtnTxt.string = "领取奖励";
          this.jsBtn.active = false;
        }
      }
    }
  }
}